import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ws2812_time/command/config.dart';

import 'logic.dart';

class ConfigPage extends StatelessWidget {
  ConfigPage({Key? key}) : super(key: key);

  final logic = Get.put(ConfigLogic());

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        TextField(
          controller: logic.host,
          decoration: const InputDecoration(labelText: "IP地址"),
        ),
        Row(
          children: [
            Expanded(
              child: TextButton(
                onPressed: () {
                  debugPrint("开始");
                  String url = "http://${logic.host.text}";
                  debugPrint(url.toString());
                  try {
                    Config.configone(url).then((value) {
                      Get.snackbar("设备连接成功！", "IP:${Config.host}\nMAC:${Config.mac}");
                    });
                  } catch (err) {
                    Get.snackbar("设备连接失败", "请检查${Config.host}");
                    debugPrint("ERROR:${err.toString()}");
                    Config.isOK.value = false;
                  }
                },
                child: Obx(
                  () => Config.isOK.value
                      ? const Text(
                          "可用",
                          style: TextStyle(color: Colors.green),
                        )
                      : const Text(
                          "连接",
                          style: TextStyle(color: Colors.blue),
                        ),
                ),
              ),
            ),
            Expanded(
              child: TextButton(
                onPressed: () {},
                child: Text("xxx"),
              ),
            ),
            Expanded(
              child: TextButton(
                onPressed: () {},
                child: Text("xxx"),
              ),
            ),
          ],
        )
      ],
    );
  }
}
